package com.jx.util;

import java.util.UUID;

import com.jx.enumType.CodePreEnum;

/**
* @Author jiangxu2
* @Description 生成编号的工具类
* @Date date()
**/
public class CodeUtil {

	public static void main(String[] args) {
		System.out.println(CodeUtil.getOrgCode("B331"));
	}

	/**
	* @Author jiangxu2
	* @Description 获取随机码
	* @Date date()
	**/
	public static String getRandomId(){
		String uuid=UUID.randomUUID().toString().replace(Constant._LINE, Constant._EMPTY);
		String date=DateUtil.getFormatTodayByFmt(DateUtil.FMT_1);
		return uuid+date;
	}
	
	/**
	* @Author jiangxu2
	* @Description 获取员工编号
	* @Date date()
	**/
	public static String getEmployeeCode(int ct){
		String suffix=String.format("%03d",ct+1);
		String dat=DateUtil.getFormatTodayByFmt(DateUtil.FMT_2);
		return "E"+dat+suffix;
	}

	/**
	* @Author jiangxu2
	* @Description 获取组织编号
	* @Date date()
	**/
	public static String getOrgCode(String oldCode){
		if(Utility.isBlank(oldCode)){
			oldCode="B000";
		}
		String head=oldCode.replaceAll("[0-9]","");
		String newstr=oldCode.replaceAll("[a-zA-Z]","").replaceAll("^(0+)", "");
		if(Utility.isBlank(newstr)){
			newstr="0";
		}
		Integer index=Integer.parseInt(newstr);
		Integer len=oldCode.length()>1?(oldCode.length()-1):2;
		String result = head+ String.format("%0" + len + "d", index + 1);
		return result;
	}
	
	public static String getGeneralCode(CodePreEnum prefix){
		int dayofyear=DateUtil.getDayOfYear();
		int yearnum=DateUtil.getYearNumStart2020();
		String dat=DateUtil.getFormatTodayByFmt(DateUtil.FMT_6);
		return prefix.getCode()+String.valueOf(yearnum)+String.valueOf(dayofyear)+dat;
	}
	
	
	public static String getGeneralCodeIncreas(CodePreEnum prefix,String oldCode,int len){
		if(Utility.isBlank(oldCode)){
			oldCode=prefix.getCode()+"000";
		}
		String newstr=oldCode.replaceAll("[a-zA-Z]","").replaceAll("^(0+)", "");
		if(Utility.isBlank(newstr)){
			newstr="0";
		}
		Integer index=Integer.parseInt(newstr);
		String result = prefix.getCode()+ String.format("%0" + len + "d", index + 1);
		return result;
	}
	
	//EST00000001
	public static String getEstateCode(String oldCode) {
		return getGeneralCodeIncreas(CodePreEnum.EST, oldCode, 8);
	}
}
